package com.wfyvv.resteasy.common.start;

import org.jboss.resteasy.plugins.server.netty.NettyJaxrsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import javax.ws.rs.Path;
import javax.ws.rs.ext.Provider;

public class NettyServer {

    @Autowired
    ApplicationContext ac;
    private NettyJaxrsServer nettyJaxrsServer;
    private int port = 8080;
    private String path = "/rest";

    public void start() {
        nettyJaxrsServer = new NettyJaxrsServer();
        // extract resources
        nettyJaxrsServer.getDeployment().getResources().addAll(
                ac.getBeansWithAnnotation(Path.class).values()
        );
        // extract providers
        nettyJaxrsServer.getDeployment().getProviders().addAll(
                ac.getBeansWithAnnotation(Provider.class).values()
        );
        // port
        nettyJaxrsServer.setPort(port);
        // rootResourcePath
        nettyJaxrsServer.setRootResourcePath(path);
        // start
        nettyJaxrsServer.start();
    }

    public void stop() {
        if (nettyJaxrsServer != null)
            nettyJaxrsServer.stop();
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setPath(String path) {
        this.path = path;
    }
}
